package com.letao.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;

import com.letao.data.AppMessage;
import com.letao.pojo.Goods;
import com.letao.pojo.Transaction;
import com.letao.pojo.User;
import com.letao.pojo.UserCollection;
import com.letao.pojo.UserFocus;
import com.letao.service.GoodsServiceInterface;
import com.letao.service.TransactionServiceInterface;
import com.letao.service.UserServiceInterface;
import com.mysql.jdbc.Blob;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserBehaviorAction extends ActionSupport {
	private UserServiceInterface userServiceInterface;
	private GoodsServiceInterface goodsServiceInterface;
	private TransactionServiceInterface transactionServiceInterface;
	
	private List<Transaction> transactionList;
	private List<Goods> goodsList;
	private List<UserFocus> userFocusList;
	private List<UserCollection> userCollectionList;
	private Goods goods;
	private User user;
	private UserFocus userFocus;
	private UserCollection userCollection;
	private int appMessage;
	//上传文件集合   
    private List<File> file;   
    //上传文件名集合   
    private List<String> fileFileName;   
    //上传文件内容类型集合   
    private List<String> fileContentType;
	public String buyGoods()throws Exception{
		Transaction transaction=new Transaction();
		transaction.setGoods(goods);
		transaction.setUser(user);
		transaction.setTstate(false);
		transactionServiceInterface.addTransaction(transaction);
		appMessage=AppMessage.BUYGOODS_SUCCESS;
		return INPUT;
	}
	public String publishGoods()throws Exception{
		goods.setGtime(new Date());
		goodsServiceInterface.addGoods(goods);
		appMessage=AppMessage.PUBLISHGOODS_SUCCESS;
		//更新session
		List<Goods> tempGoodsList=goodsServiceInterface.getGoodsByOwer(goods.getUser().getUid());
		if(tempGoodsList!=null&&tempGoodsList.size()>=0){
		Integer maxGid=-1;
		for(Goods tempGoods:tempGoodsList){
			if(tempGoods.getGid()>maxGid) goods=tempGoods;
		}
		ActionContext.getContext().getSession().put("recentgid", goods.getGid());
		}
		//
		return INPUT;
	}
	public String uploadImage()throws Exception{
		goodsServiceInterface.addGoodsImage(goods, file);
		appMessage=AppMessage.UPLOAD_SUCCESS;
		return INPUT;
	}
	
	public String getLastPublish()throws Exception{
		List<Goods> tempGoodsList=goodsServiceInterface.getGoodsByOwer(user.getUid());
		Integer maxGid=-1;
		for(Goods tempGoods:tempGoodsList){
			if(tempGoods.getGid()>maxGid) goods=tempGoods;
		}
		ActionContext.getContext().getSession().put("recentgid", goods.getGid());
		return INPUT;
	}
	public String addUserFocus()throws Exception{
		userServiceInterface.addUserFocus(userFocus);
		appMessage=AppMessage.ADDUSERFOCUS_SUCCESS;
		return INPUT;
	}
	public String deleteUserFocus()throws Exception{
		userServiceInterface.deleteUserFocus(userFocus);
		appMessage=AppMessage.DELETEUSERFOCUS_SUCCESS;
		return INPUT;
	}
	public String addCollection()throws Exception{
		goodsServiceInterface.addCollection(userCollection);
		appMessage=AppMessage.ADDCOLLECTION_SUCCESS;
		return INPUT;
	}
	public String deleteCollection()throws Exception{
		goodsServiceInterface.deleteCollection(userCollection);
		appMessage=AppMessage.DELETECOLLECTION_SUCCESS;
		return INPUT;
	}
	public String getTransactionByUser()throws Exception{
		transactionList=transactionServiceInterface.getTransactionByUser(user.getUid());
		return INPUT;
	}
	public String getGoodsByOwer()throws Exception{
		goodsList=goodsServiceInterface.getGoodsByOwer(user.getUid());
		return INPUT;
	}
	public String getUserFocusByUser()throws Exception{
		userFocusList=userServiceInterface.getUserFocusByUser(user.getUid());
		return INPUT;
	}
	public String getUserCollectionByUser()throws Exception{
		userCollectionList=goodsServiceInterface.getUserCollectionByUser(user.getUid());
		return INPUT;
	}
	public UserServiceInterface getUserServiceInterface() {
		return userServiceInterface;
	}
	public void setUserServiceInterface(UserServiceInterface userServiceInterface) {
		this.userServiceInterface = userServiceInterface;
	}
	public GoodsServiceInterface getGoodsServiceInterface() {
		return goodsServiceInterface;
	}
	public void setGoodsServiceInterface(GoodsServiceInterface goodsServiceInterface) {
		this.goodsServiceInterface = goodsServiceInterface;
	}
	public TransactionServiceInterface getTransactionServiceInterface() {
		return transactionServiceInterface;
	}
	public void setTransactionServiceInterface(
			TransactionServiceInterface transactionServiceInterface) {
		this.transactionServiceInterface = transactionServiceInterface;
	}
	public List<Transaction> getTransactionList() {
		return transactionList;
	}
	public void setTransactionList(List<Transaction> transactionList) {
		this.transactionList = transactionList;
	}
	public List<Goods> getGoodsList() {
		return goodsList;
	}
	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}
	public List<UserFocus> getUserFocusList() {
		return userFocusList;
	}
	public void setUserFocusList(List<UserFocus> userFocusList) {
		this.userFocusList = userFocusList;
	}
	public List<UserCollection> getUserCollectionList() {
		return userCollectionList;
	}
	public void setUserCollectionList(List<UserCollection> userCollectionList) {
		this.userCollectionList = userCollectionList;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserFocus getUserFocus() {
		return userFocus;
	}
	public void setUserFocus(UserFocus userFocus) {
		this.userFocus = userFocus;
	}
	public UserCollection getUserCollection() {
		return userCollection;
	}
	public void setUserCollection(UserCollection userCollection) {
		this.userCollection = userCollection;
	}
	public int getAppMessage() {
		return appMessage;
	}
	public void setAppMessage(int appMessage) {
		this.appMessage = appMessage;
	}
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	
}
